/**
 * 
 */
package br.com.cnpv.modelo.bo;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;

import br.com.cnpv.modelo.acesso.idao.IClienteDAO;
import br.com.cnpv.modelo.dao.ClienteDAO;
import br.com.cnpv.modelo.entity.Cliente;
import br.com.cnpv.util.HibernateUtil;

/**
 * @author ricardo
 *
 */
public class ClienteBO extends HibernateUtil {

	public static ClienteBO INSTANCE;
	
	public static ClienteBO getInstance()
	{
		if(INSTANCE == null)
		{
			synchronized (ClienteBO.class) {
				INSTANCE = new ClienteBO();
			}
		}
		return INSTANCE;
	}

	public List<Cliente> buscar(Cliente cliente) throws Exception
	{
		Session session = null;
		List<Cliente> list = null;
		try
		{
			session = currentSession();
			IClienteDAO dao = new ClienteDAO();
			list = dao.buscar(cliente, session);
		}catch(Exception e)
		{
			throw new Exception(e);
		}finally
		{
			if(session != null)
			{
				session.flush();
				session.clear();
				closeSession();
			}
		}
		return list;
	}

	public Cliente saveOrUpdate(Cliente cliente) throws Exception{
		Session session = null;
		try
		{
			session = currentSession();
			IClienteDAO dao = new ClienteDAO();
			cliente.setDtIncl(new Date());
			
			cliente = dao.saveOrUpdate(cliente, session);
		}catch(Exception e)
		{
			throw new Exception(e);
		}finally
		{
			if(session != null)
			{
				session.flush();
				session.clear();
				closeSession();
			}
		}
	
		return cliente;
	}

	public List<Cliente> listar() throws Exception
	{
		Session session = null;
		List<Cliente> list = null;
		try
		{
			session = currentSession();
			IClienteDAO dao = new ClienteDAO();
			list = dao.listar(session);
		}catch(Exception e)
		{
			throw new Exception(e);
		}finally
		{
			if(session != null)
			{
				session.flush();
				session.clear();
				closeSession();
			}
		}
		return list;
	}

}
